<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $res = DB::table('news')->get();
        return view('news.index',['res'=>$res]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('news.add');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $res = $request->except('_token','logo');

        if($request->hasFile('logo')){

            $name = time().rand(1000,9999);
            $suffix = $request->file('logo')->getClientOriginalExtension();

            $request->file('logo')->move('./upload',$name.'.'.$suffix);

            $res['logo'] = './upload/'.$name.'.'.$suffix;
        }

        $res['addtime'] = date('Y-m-d H:i:s',time());
        $data = DB::table('news')->insert($res);
        if($data){
            return redirect('/home/news');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $res = DB::table('news')->where('id',$id)->first();

        return view('news.edit',['res'=>$res]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
         $res = $request->except('_token','logo','_method');

        if($request->hasFile('logo')){

            $name = time().rand(1000,9999);
            $suffix = $request->file('logo')->getClientOriginalExtension();

            $request->file('logo')->move('./upload',$name.'.'.$suffix);

            $res['logo'] = '/upload/'.$name.'.'.$suffix;
        }

        $res['addtime'] = date('Y-m-d H:i:s',time());
        $data = DB::table('news')->where('id',$id)->update($res);
        if($data){
            return redirect('/home/news');
        }else{
            return back();
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
       $res = DB::table('news')->where('id',$id)->delete();

       if(!$res){
        return redirect('/home/news');
       }else{
        return back();
       }
    }
}
